//
//  JCBaseTabBarController.swift
//  swiftWeibo
//
//  Created by mac on 15/12/15.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit

class JCBaseTabBarController: UITabBarController {

    
    override class func initialize() {
        
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        let homeVc = JCHomeViewController()
        setupVc(homeVc, title: "主页", imgName: "tabbar_home")
        
        // 消息
        let messageVC = JCMessageViewController()
        setupVc(messageVC, title: "消息", imgName: "tabbar_message_center")
        
        // 占位控制器
        addChildViewController(UIViewController())
        
        // 发现
        let discoverVC = JCDiscoveryViewController()
        setupVc(discoverVC, title: "发现", imgName: "tabbar_discover")
        
        // 我
        let profileVC = JCProfileViewController()
        setupVc(profileVC, title: "我", imgName: "tabbar_profile")
        
    }
    
    private func setupVc(vc:UIViewController ,title:String ,imgName:String){
        
        addChildViewController(JCBaseNavController(rootViewController:vc))
        vc.tabBarItem.title = title
        vc.tabBarItem.image = UIImage(named: imgName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        let highLightImg = imgName + "_highlighted"
        vc.tabBarItem.selectedImage = UIImage(named: highLightImg)?.imageWithRenderingMode(.AlwaysOriginal)
        
    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        // 设置撰写按钮的frame
        let width = tabBar.bounds.size.width / 5
        let frame = CGRect(x: 2 * width - 5, y: 0, width: width + 10, height: tabBar.bounds.height)
        composeBtn.frame = frame
    }
    
    
    lazy var composeBtn: UIButton = {
        
        let button = UIButton()
        
        // 设置参数
        // 设置按钮背景图片
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        // 设置按钮图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        // 添加到tabbar里面
        self.tabBar.addSubview(button)
        
        // 添加点击事件
        button.addTarget(self, action: "composeClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    func composeClick(){
    
        print("111")
    }
}
